<script setup>
import { oneDark } from '@codemirror/theme-one-dark'
import CodeMirror from 'vue-codemirror6'
import { java } from '@codemirror/lang-java'
import { json } from '@codemirror/lang-json'

const props = defineProps({
  lang: {
    type: String,
    default: 'java',
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  value: {
    type: String,
    default: '',
  },
})
const emit = defineEmits(['update:value'])
const extensions = [oneDark]
const code = ref(props.value) // 初始化code为props.value的值

const langConfigMap = {
  json: [json()],
  java: [java()],
}

const codeLang = computed(() => {
  return langConfigMap[props.lang] || []
})

watch(() => props.value, (newValue) => {
  code.value = newValue
})
emit('update:value', code.value)
</script>

<template>
  <CodeMirror
    v-model="code"
    basic
    :lang="codeLang"
    :extensions="extensions"
    :disabled="disabled"
  />
</template>

<style lang="scss" scoped>

</style>
